﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Ue.Infrastructure;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.Permissions;
using Ue.Qcdz.Domain.Roles;
using Ue.Framework.Data;
using Ue.Qcdz.Repositories.Membership;
using Ue.Qcdz.Repositories.Roles;
using Ue.Qcdz.UI.Controls.Permission;

namespace Ue.Qcdz.Repositories.Permissions
{
    public class PermissionRepository : SqlServerRepository<Permission>, IPermissionsRepository
    {
        public PermissionRepository()
            : this(null) { }

        public PermissionRepository(IUnitOfWork uniteOfWork)
            : base(uniteOfWork)
        {

        }
        protected override string GetDataTableName()
        {
            return "Permissions";
        }

        protected override string GetKeyFieldName()
        {
            return PermissionFactory.FieldNames.EntityKey;
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[]
            {
                PermissionFactory.FieldNames.EntityKey,
                PermissionFactory.FieldNames.FeatureName,
                PermissionFactory.FieldNames.Name,
                PermissionFactory.FieldNames.Description,
            };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[]
            {
                PermissionFactory.FieldNames.FeatureName,
                PermissionFactory.FieldNames.Name,
                PermissionFactory.FieldNames.Description,
            };
        }

        protected override IDataParameter[] BuildParameters(Permission entity)
        {
            return new IDataParameter[]
            {
                RepositoryDriver.BuildParameter(PermissionFactory.FieldNames.EntityKey,entity.Key),  
                RepositoryDriver.BuildParameter(PermissionFactory.FieldNames.Name,entity.Name),
                RepositoryDriver.BuildParameter(PermissionFactory.FieldNames.FeatureName,entity.FeatureName),
                RepositoryDriver.BuildParameter(PermissionFactory.FieldNames.Description,entity.Description),
            };
        }

        protected override string GetOrderBy()
        {
            return null;
        }

        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            PermissionGridView permissionGridView = dataQuery as PermissionGridView;
            if (permissionGridView != null)
            {

                return new PermissionWhereClause(permissionGridView);

            }
            return null;
        }

        protected override IDictionary<string, ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            return null;
        }

        public Permission FindBy(string permissionsName)
        {
            return null;
        }
        protected override void PersistDeletedItem(Permission item)
        {
            base.PersistDeletedItem(item);
            var query = "DELETE FROM RoleInPermissions WHERE PermissionId=@PermissionId";
            RepositoryDriver.ExecuteNonQuery(
                 this.RepositoryDriver.BuildCommand(
                    query,
                    this.RepositoryDriver.BuildParameter("PermissionId", item.Key)
                    ));

        }
        public IList<Permission> FindBy(Role role)
        {
            var query =
                "select * from Permissions where exists(select * from RoleInPermissions where RoleInPermissions.PermissionId=Permissions.Id and RoleInPermissions.RoleId=@RoleId)";
            return this.BuildEntitiesFromSql(query, this.RepositoryDriver.BuildParameter("@RoleId", role.Key));
        }

        public void Delete(Role role)
        {
            var relating = new Relating("RoleInPermissions", RoleFactory.FieldNames.PermissionFieldNames.RoleId, RoleFactory.FieldNames.PermissionFieldNames.PermissionsId);
            var relatingBase = new RelatingBase(relating);
            foreach (var permission in role.PermissionList)
            {
                relatingBase.DeleteRelating(permission.Key.ToString(), role.Key.ToString());
            }
        }

        public class PermissionWhereClause : IWhereClause
        {
            private PermissionGridView permissionGridView;

            public PermissionWhereClause(PermissionGridView permissionGridView)
            {
                this.permissionGridView = permissionGridView;
            }

            public string Clause
            {

                get
                {
                    var clause = "Name LIKE '%'+@Name+'%' AND FeatureName LIKE '%'+@FeatureName+'%' AND Description LIKE '%'+@Description+'%'";
                    return clause;
                }

            }

            public IDataParameter[] Parameters
            {

                get
                {
                    string strName = permissionGridView.Name;
                    if (string.IsNullOrEmpty(strName)) strName = "";
                    string strFeatureName = permissionGridView.FeatureName;
                    if (string.IsNullOrEmpty(strFeatureName)) strFeatureName = "";
                    string strDescription = permissionGridView.Description;
                    if (string.IsNullOrEmpty(strDescription)) strDescription = "";

                    var Parameters = new IDataParameter[]
                                         {
                                             SqlServerDriver.Instance.BuildParameter(PermissionFactory.FieldNames.Name,
                                                                                     strName),
                                             SqlServerDriver.Instance.BuildParameter(PermissionFactory.FieldNames.FeatureName,
                                                                                     strFeatureName),
                                             SqlServerDriver.Instance.BuildParameter(PermissionFactory.FieldNames.Description,
                                                                                   strDescription)
                                     };
                    return Parameters;
                }

            }
        }
    }
}
